package com.ericbutler.info;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.ericbutler.info.dao.TestTableDAO;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {

	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
	@Resource
	private TestTableDAO testTableDAO;

	/** Simply selects the home view to render by returning its name. */
	@RequestMapping(value = "/home")
	public ModelAndView home(Locale locale, Model model) {
		logger.info("Welcome home! the client locale is " + locale.toString());

		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
		String formattedDate = dateFormat.format(date);

		model.addAttribute("serverTime", formattedDate);
		return new ModelAndView("home", model.asMap());
	}

	@RequestMapping(value = { "/", "/start.action" })
	public ModelAndView start(HttpServletRequest request) {
		logger.info("Starting!");
		return new ModelAndView("start", null);
	}

	@RequestMapping(value = "/aboutMe.action")
	public ModelAndView aboutMe(HttpServletRequest request) {
		logger.info("Loading AboutMe...");
		return new ModelAndView("content/aboutMe", null);
	}

	@RequestMapping(value = "/resume.action")
	public ModelAndView resume(HttpServletRequest request) {
		logger.info("Loading Resume...");
		return new ModelAndView("content/resume", null);
	}

	@RequestMapping(value = "/examples.action")
	public ModelAndView examples(HttpServletRequest request) {
		logger.info("Loading Examples...");
		ModelAndView mv = new ModelAndView("content/examples", null);
		mv.addObject("testRecords", testTableDAO.getAllTestRecords());
		return mv;
	}

	@RequestMapping(value = "/recreations.action")
	public ModelAndView recreations(HttpServletRequest request) {
		logger.info("Loading Recreations...");
		return new ModelAndView("content/recreations", null);
	}
}
